package com.xhd.im;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;
import com.xhd.base.session.Session;
import com.xhd.base.session.SessionManager;
import com.xhd.server.ServerConfig;
import com.xhd.server.XServerId;
import com.xhd.serverComm.DataTranCenter;
import com.xhd.util.XThread;

public class OnlineMgr {
	private static OnlineMgr jm16 = null;
	  private WaitConnectList jm211 = null;
	  private HashMap jm212 = null;
	  private SessionManager jm213 = null;
	  private String jm84 = null;
	  private static final Log jm9;
	  static Class jm10;

	  static
	  {
	    Class tmp7_4 = jm10;
	    if (tmp7_4 == null)
	    {
	 
	      try
	      {
	      OnlineMgr.jm10 = Class.forName("com.sinocc.base.im.OnlineMgr");
	      }
	      catch (ClassNotFoundException localClassNotFoundException)
	      {
	        throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
	      }
	    }
	    jm9 = LogFactory.getLog(tmp7_4);
	  }

	  public static OnlineMgr getInstance()
	  {
	    if (jm16 == null)
	      jm16 = new OnlineMgr();
	    return jm16;
	  }

	  public void init()
	  {
	    Vector localVector = this.jm213.getOnlineUsers(0);
	    if ((localVector == null) || (localVector.size() < 1))
	    {
	      jm9.trace("Online old count: none.");
	      return;
	    }
	    String str = XServerId.getLocal().getId();
	    int i = localVector.size();
	    int j = 0;
	    for (int k = 0; k < i; ++k)
	    {
	      Session localSession = (Session)localVector.get(k);
	      if (!(str.equals(localSession.getMsgServer())))
	        continue;
	      ++j;
	      this.jm211.add(localSession.getId());
	    }
	    jm9.trace("Online old count: " + j);
	  }

	  public void clientConnected(String paramString)
	  {
	    this.jm211.remove(paramString);
	    if (!(this.jm213.setOnline(paramString, true)))
	      return;
	    onChanged(paramString, true);
	  }

	  public void clientDisConnected(String paramString)
	  {
	    this.jm211.add(paramString);
	  }

	  public int getOnlineCount()
	  {
	    return (int)ServerConfig.OnlineCount;
	  }

	  public Vector getOnlines(int paramInt)
	  {
	    Vector localVector = this.jm213.getOnlineUsers(paramInt);
	    if (localVector != null)
	    {
	      int i = localVector.size();
	      jm9.trace("Get online user: " + i);
	      ServerConfig.OnlineCount = i;
	    }
	    return localVector;
	  }

	  public void onChanged(String paramString, boolean paramBoolean)
	  {
	    this.jm212.put(paramString, (paramBoolean) ? this.jm84 : "");
	  }

	  private HashMap jm210()
	  {
	    HashMap localHashMap = this.jm212;
	    if (localHashMap.size() > 0)
	    {
	      this.jm212 = new HashMap();
	      return localHashMap;
	    }
	    return null;
	  }

	  public void setOnlines(Map paramMap)
	  {
	    if ((paramMap == null) || (paramMap.size() < 1))
	      return;
	    SessionManager localSessionManager = SessionManager.getInstance();
	    Iterator localIterator = paramMap.keySet().iterator();
	    int i = 0;
	    while (localIterator.hasNext())
	    {
	      String str1 = (String)localIterator.next();
	      Session localSession = localSessionManager.getSessionById(str1);
	      if (localSession == null)
	        continue;
	      ++i;
	      String str2 = (String)paramMap.get(str1);
	      if (str2.length() > 1)
	        localSession.setOnline(str2);
	      else
	        localSession.setOnline(false);
	    }
	    jm9.trace("setOnlines count=" + i);
	  }

	  protected void syncOnline()
	  {
	    if (ServerConfig.MultiMsgServer)
	    {
	     HashMap localObject = jm210();
	      if ((localObject != null) && (((HashMap)localObject).size() > 0))
	      {
	    	  XServerId    localXServerId = XServerId.getLocal(2);
	        DataTranCenter.addSendData(localXServerId.getId(), "<online>", localObject);
	      }
	    }
	    Object localObject = new Vector();
	    ((Vector)localObject).add(String.valueOf(ServerConfig.OnlineCount));
	    XServerId localXThread = XServerId.getLocal(1);
	    DataTranCenter.addSendReq(localXThread.getId(), "<actiontime>", (Vector)localObject);
	  }	  
}
